package com.ruoyi.lock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.SysSetting;
import com.ruoyi.lock.service.ISysSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统设置Controller
 * 
 * @author arvin
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/lock/setting")
public class SysSettingController extends BaseController
{
    @Autowired
    private ISysSettingService sysSettingService;

    /**
     * 查询系统设置列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSetting sysSetting)
    {
        startPage();
        List<SysSetting> list = sysSettingService.selectSysSettingList(sysSetting);
        return getDataTable(list);
    }

    /**
     * 导出系统设置列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:export')")
    @Log(title = "系统设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSetting sysSetting)
    {
        List<SysSetting> list = sysSettingService.selectSysSettingList(sysSetting);
        ExcelUtil<SysSetting> util = new ExcelUtil<SysSetting>(SysSetting.class);
        util.exportExcel(response, list, "系统设置数据");
    }

    /**
     * 获取系统设置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId)
    {
        return success(sysSettingService.selectSysSettingByDeptId(deptId));
    }

    /**
     * 新增系统设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:add')")
//    @Log(title = "系统设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSetting sysSetting)
    {
        SysSetting sysSetting1 = sysSettingService.selectSysSettingByDeptId(sysSetting.getDeptId());
        if(sysSetting1 ==null){
            return toAjax(sysSettingService.insertSysSetting(sysSetting));
        }else{

            return toAjax(sysSettingService.updateSysSetting(sysSetting));
        }


    }

    /**
     * 修改系统设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:edit')")
    @Log(title = "系统设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSetting sysSetting)
    {
        return toAjax(sysSettingService.updateSysSetting(sysSetting));
    }

    /**
     * 删除系统设置
     */
//    @PreAuthorize("@ss.hasPermi('lock:setting:remove')")
    @Log(title = "系统设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(sysSettingService.deleteSysSettingByDeptIds(deptIds));
    }
}
